package com.snow.platform.login.controller;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.snow.platform.auth.po.Menu;
import com.snow.platform.auth.po.Role;
import com.snow.platform.auth.service.api.RoleService;
import com.snow.platform.login.po.Users;
import com.snow.platform.login.service.api.UsersService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.List;

@Controller
public class LoginController {

    private static Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private UsersService usersService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private JedisConnectionFactory jedisConnectionFactory;

    @RequestMapping(value = "/doLogin", method = RequestMethod.POST)
    @ResponseBody
    public Users doLogin(String username, String password){

        logger.error(jedisConnectionFactory.getHostName() + ":" + jedisConnectionFactory.getPort());

        logger.error(Thread.currentThread().getName());

        List<Role> roleList = roleService.selectList(null);

        logger.error(roleList.get(0).toString());

        List<Users> usersList = usersService.selectList(null);
        Users users = usersList.get(0);
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password);

        Users users1 = new Users();
        users1.setId(2L);
//        users1.setUserNo("3");
        users1.setPassword("3");

        Users users2 = new Users();
        users2.setId(3L);
        users2.setUserNo("3");
        users2.setPassword("3");

        List<Users> usersList1 = new ArrayList<>();
        usersList1.add(users1);
        usersList1.add(users2);
        Wrapper<Users> usersWrapper = Condition.create();
        usersWrapper.orderBy("ss", false);
        usersService.updateBatchById(usersList1);

        try {
            SecurityUtils.getSubject().login(usernamePasswordToken);
        }catch (UnknownAccountException e){
            logger.error("账号不正确");
        }catch (IncorrectCredentialsException e){
            logger.error("密码不正确");
        }
        return usersList.get(0);
    }

    @RequestMapping(value = "/add")
    @ResponseBody
    @RequiresPermissions("add")
    public String add(){
        logger.error("add");
        return "add";
    }

    @RequestMapping(value = "/login")
    public String login(){
        return "login";
    }


    @RequestMapping(value = "/index")
    public String index(){
        return "index";
    }

    @RequestMapping(value = "/menuIndex")
    public String menu(){
        return "menu";
    }
}
